package com.ittao.taoaicodeapp.controller;

import com.ittao.taoaicodeapp.service.VipLimitService;
import com.ittao.taoaicodeclient.innerservice.InnerUserService;
import com.ittao.taoaicodecommon.common.BaseResponse;
import com.ittao.taoaicodecommon.common.ResultUtils;
import com.ittao.taoaicodemodel.model.entity.User;
import com.ittao.taoaicodemodel.model.enums.UserRoleEnum;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * VIP 限制 控制层
 *
 * @author <a href="https://gitee.com/it-heima-ht">程序员 Tao</a>
 */
@RestController
@RequestMapping("/vip")
public class VipLimitController {

    @Resource
    private VipLimitService vipLimitService;

    /**
     * 获取当前用户的VIP限制信息
     *
     * @param request 请求
     * @return VIP限制信息
     */
    @GetMapping("/limits")
    public BaseResponse<Map<String, Object>> getUserLimits(HttpServletRequest request) {
        User loginUser = InnerUserService.getLoginUser(request);

        Map<String, Object> result = new HashMap<>();

        // 基本用户信息
        String userRole = loginUser.getUserRole();
        UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValue(userRole);
        result.put("userRole", userRole);
        result.put("isVip", UserRoleEnum.VIP.equals(userRoleEnum) || UserRoleEnum.ADMIN.equals(userRoleEnum));

        // 获取配置限制
        int maxTokens = vipLimitService.getMaxTokens(loginUser);
        int maxMessages = vipLimitService.getMaxMessages(loginUser);
        result.put("maxTokens", maxTokens);
        result.put("maxMessages", maxMessages);

        // 获取今日使用情况
        String remainingInfo = vipLimitService.getRemainingLimitsInfo(loginUser);
        result.put("remainingInfo", remainingInfo);

        return ResultUtils.success(result);
    }
}
